﻿using Basic.WxApiRemoteModel;
using WeDonekRpc.Helper;

namespace Basic.WxService.Model.WxBind
{
    public class WxMerBindCache
    {
        /// <summary>
        /// 申请ID
        /// </summary>
        public string ApplyId
        {
            get;
            set;
        }
        /// <summary>
        /// 平台AppId
        /// </summary>
        public string AppId
        {
            get;
            set;
        }
        /// <summary>
        /// 绑定的状态
        /// </summary>
        public WxBindStatus BindStatus
        {
            get;
            set;
        }
        /// <summary>
        /// 回调地址
        /// </summary>
        public string CallbackUri
        {
            get;
            set;
        }
        /// <summary>
        /// 预授权码
        /// </summary>
        public string PreauthCode
        {
            get;
            set;
        }

        /// <summary>
        /// 错误码
        /// </summary>
        public string Error
        {
            get;
            set;
        }
        /// <summary>
        /// 绑定的微信应用ID
        /// </summary>
        public string WxAppId
        {
            get;
            set;
        }
        /// <summary>
        /// 绑定类型
        /// </summary>
        public WxBindType AuthType
        {
            get;
            set;
        }
        /// <summary>
        /// 回复设置
        /// </summary>
        public RpcReplySet ReplySet { get; set; }
        public DateTime? OverTime { get; set; }

        public bool CheckState (WxBindStatus state)
        {
            if (this.BindStatus == WxBindStatus.绑定失败)
            {
                throw new ErrorException(this.Error);
            }
            else if (this.BindStatus == WxBindStatus.绑定完成)
            {
                throw new ErrorException("wx.apply.complete");
            }
            return state == this.BindStatus;
        }
        public string GetCallbackUri ()
        {
            string query;
            if (this.BindStatus == WxBindStatus.绑定完成)
            {
                query = string.Format("appId={0}&status=3&authType={1}&applyId={2}", this.WxAppId, (int)this.AuthType, this.ApplyId);
            }
            else if (this.BindStatus == WxBindStatus.绑定失败)
            {
                query = string.Format("status=4&authType={0}&applyId={1}&error={2}", (int)this.AuthType, this.ApplyId, this.Error);
            }
            else
            {
                query = string.Format("status={0}&authType={1}&applyId={2}", (int)this.BindStatus, (int)this.AuthType, this.ApplyId);
            }
            return Tools.GetJumpUri(this.CallbackUri, query);
        }
    }
}
